/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.lyqg.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.core.freemarker.HtmlPathHelp;
import com.core.orm.Page;
import com.core.orm.PageRequest;
import com.core.utils.WebUtil;
import com.lyqg.biz.IContentService;
import com.lyqg.biz.ICouponService;
import com.lyqg.biz.IMallService;
import com.lyqg.domain.Content;
import com.lyqg.domain.Coupon;
import com.lyqg.domain.Mall;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-8-14 下午08:14:05
 */
@Controller
public class MallAction extends BaseAction {

	@Resource
	IMallService mallService;
	@Resource
	IContentService contentService;
	@Resource
	ICouponService couponService;

	@RequestMapping(value = "/mall/all.html")
	public String mallindex(HttpServletRequest request, ModelMap model) {
		model.addAttribute("currentMenuMall", "currentMenu");
		return HtmlPathHelp.getHtmlPathFront("mallall");
	}

	@RequestMapping(value = "/mall/go.html")
	public String mallgo(HttpServletRequest request) {
		String ename = WebUtil.getString(request, "ename");
		String goUrl = mallService.getGoUrl(ename);
		if (StringUtils.isNotBlank(goUrl)) {
			return "redirect:" + goUrl;
		}
		return "redirect:/";
	}

	@RequestMapping(value = "/mall/index.html")
	public String mall(HttpServletRequest request, ModelMap model) {
		String ename = WebUtil.getString(request, "pename");
		Mall mall = mallService.getMallByEname(ename);
		if (mall == null) {
			return "redirect:/404.htm";
		}
		model.addAttribute("mall", mall);
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 2);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Content> mallContent = contentService.queryByMall(pageRequest,
				ename);
		model.addAttribute("mallContent", mallContent);
		List<Coupon> listCoupons = couponService.queryByMallId(mall.getId());
		if (listCoupons != null && listCoupons.size() > 0) {
			model.addAttribute("listCoupons", listCoupons);
		}
		model.addAttribute("currentMenuMall", "currentMenu");
		return HtmlPathHelp.getHtmlPathFront("mall");
	}

}
